package com.youlin.controller;

import com.youlin.dto.BaseDataResDto;
import com.youlin.dto.BaseReqDto;
import com.youlin.dto.BaseResDto;
import com.youlin.dto.Status;
import com.youlin.dto.req.UpdPwdReqDto;
import com.youlin.dto.req.WxShareReqDto;
import com.youlin.dto.res.DeadManResDto;
import com.youlin.dto.res.OSSResDto;
import com.youlin.dto.res.WxShareResDto;
import com.youlin.service.BygAdminService;
import com.youlin.service.BygDeadManService;
import com.youlin.untils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Constructor;

@ApiSort(2)
@Api(tags = {"2 - 通用接口"})
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private WxUtil wxUtil;
    @Autowired
    private OSSUtil ossUtil;
    @Autowired
    private BygAdminService bygAdminService;
    @Autowired
    private BygDeadManService bygDeadManService;

    @UnIntercept
    @PostMapping("/toLogin")
    @ApiOperation(value = "", hidden = true)
    public BaseResDto toLogin () {
        return new BaseResDto(Status.UNLOGIN);
    }

    @UnIntercept
    @PostMapping("/updPwdFirst")
    @ApiOperation(value = "", hidden = true)
    public BaseResDto updPwdFirst () {
        return new BaseResDto<>(Status.UPDPWDFIRST);
    }

    @UnIntercept
    @PostMapping("/tokenLogin")
    @ApiOperation(value = "", hidden = true)
    public <T> T tokenLogin (HttpServletRequest request) {
        Constructor<?> constructor = (Constructor<?>) request.getAttribute("Constructor");
        try {
            return (T) constructor.newInstance(Status.NEEDTAKERLOGIN);
        } catch (Exception e) {
            return (T) new BaseResDto(Status.ERROR);
        }
    }

    /**
     * 单位登录用户修改密码
     * @param baseReqDto
     * @return
     */
    @UnIntercept
    @PostMapping("/updPwd")
    @ApiOperationSort(value = 100)
    @ApiOperation(value = "100 - 单位登录用户修改密码", notes = "响应参数：status-message = {" +
            "1012-密码必须为8-20位数字和字母组合, 1013-原密码错误}")
    public BaseResDto updPwd (@RequestBody BaseReqDto<UpdPwdReqDto> baseReqDto) {
        UpdPwdReqDto data = baseReqDto.getData();
        if (MyBeanUtil.isParamError(data)) return new BaseResDto(Status.PARAMETERERROR);
        return bygAdminService.updPwd (data.getAdminId(), data.getOldPwd(), data.getNewPwd());
    }

    /**
     * 获取单位登录验证码base64数据
     * @return
     */
    @UnIntercept
    @GetMapping("/guest/getCaptchaBase64")
    @ApiOperationSort(value = 200)
    @ApiOperation(value = "200 - 获取单位登录验证码base64数据")
    public BaseDataResDto<String> getCaptchaBase64 () {
        return bygAdminService.getCaptchaBase64 ();
    }

    /**
     * 校验单位登录验证码
     * @param baseReqDto
     * @return
     */
    @UnIntercept
    @PostMapping("/guest/validateCaptcha")
    @ApiOperationSort(value = 300)
    @ApiOperation(value = "300 - 校验单位登录验证码", notes = "请求参数data = 验证码")
    public BaseResDto validateCaptcha (@RequestBody BaseReqDto<String> baseReqDto) {
        String code = baseReqDto.getData();
        return bygAdminService.validateCaptcha (code);
    }

    /**
     * 获取OSS上传签名
     * @return
     */
    @PostMapping("/getOSSsignature")
    @ApiOperationSort(value = 400)
    @ApiOperation(value = "400 - 获取OSS上传签名", notes = "请求参数参数：上传逝者相关照片时data=\"deadMan\"，\n" +
            "上传风俗信息相关图片或视频时data=\"custom\"")
    public BaseDataResDto<OSSResDto> getOSSsignature (@RequestBody BaseReqDto<String> baseReqDto) {
        String dir = baseReqDto.getData();
        OSSResDto ossResDto = ossUtil.getOSSsignature(dir);
        return new BaseDataResDto<OSSResDto>(Status.SUCCESS).setData(ossResDto);
    }

    /**
     * OSS上传文件
     * @return
     */
    @PostMapping("/ossUpload")
    @ApiOperationSort(value = 500)
    @ApiOperation(value = "500 - OSS上传文件", notes = "响应参数：data=上传文件url")
    public BaseDataResDto<String> upload (@RequestBody MultipartFile uploadFile) {
        String url = ossUtil.upload(uploadFile);
        if (StringUtils.isBlank(url)) return new BaseDataResDto<>(Status.ERROR);
        return new BaseDataResDto<>(Status.SUCCESS);
    }

    /**
     * 微信公众号token验证
     * @return
     */
    @GetMapping("/guest/wxTokenValid")
    @ApiOperation(value = "微信公众号token验证", hidden = true)
    public String wxTokenValid (HttpServletRequest request) throws IOException {
        String msgSignature = request.getParameter("signature");
        String msgTimestamp = request.getParameter("timestamp");
        String msgNonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        /*if (WXPublicUtils.verifyUrl(msgSignature, msgTimestamp, msgNonce)) {
            return echostr;
        }*/
        return echostr;
    }

    /**
     * 获取微信分享签名
     * @param baseReqDto
     * @return
     */
    @PostMapping("/guest/getWxShare")
    @ApiOperationSort(value = 600)
    @ApiOperation(value = "600 - 获取微信分享签名")
    public BaseDataResDto<WxShareResDto> getWxShare(@RequestBody BaseReqDto<WxShareReqDto> baseReqDto) {
        WxShareReqDto data = baseReqDto.getData();
        if (MyBeanUtil.isParamError(data)) return new BaseDataResDto<>(Status.PARAMETERERROR);
        WxShareResDto wx = wxUtil.getWxShare(data.getUrl());
        BaseDataResDto<DeadManResDto> baseDataResDto = bygDeadManService.findDeadManByAmUuId(data.getAmUuId());
        DeadManResDto dataResDtoData = baseDataResDto.getData();
        wx.setDmName(dataResDtoData.getDeadName()).setDnGender(dataResDtoData.getDeadGender())
                .setDmHeadUrl(dataResDtoData.getHeadUrl());
        return new BaseDataResDto<WxShareResDto>(Status.SUCCESS).setData(wx);
    }

}
